<?php

namespace addons\cardocr;

use addons\cardocr\library\Card;
use app\common\library\Menu;
use think\Addons;
use think\Exception;
use think\Loader;
use think\Request;

/**
 * 身份证联网认证插件
 */
class Cardocr extends Addons
{
    private $menu;
    private $name = "cardocr";

    public function __construct()
    {
        parent::__construct();
        $menu = [
            [
                'name'    => 'cardocr',
                'title'   => '身份证验证管理',
                'icon'    => 'fa fa-address-book',
                'sublist' => [
                    [
                        'name'    => 'cardocr/card',
                        'title'   => '身份证验证',
                        'icon'    => 'fa  fa-id-card',
                        'sublist' => [
                            ['name' => 'cardocr/card/index', 'title' => '查看'],
                            ['name' => 'cardocr/card/add', 'title' => '添加'],
                            ['name' => 'cardocr/card/edit', 'title' => '修改'],
                            ['name' => 'cardocr/card/del', 'title' => '删除'],
                            ['name' => 'cardocr/card/multi', 'title' => '批量更新'],
                        ]
                    ],

                ]
            ]
        ];

        $this->menu = $menu;
    }

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        Menu::create($this->menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete($this->name);
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable($this->name);
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable($this->name);
        return true;
    }

    /** 检查身份证号码与姓名是一致钩子
     * @param array $checkdata ['name'];$checkdata["idnum"]
     * @return bool
     */
    public function checkIdname($checkdata)
    {
        $arr['time'] = time();
        $data = Card::checkidcard($checkdata["name"], $checkdata["idnum"]);
        if ($data['Result'] < 0) {
            $arr["msg"] = $data['Description'];
            $arr["data"] = $data;
            echo json_encode($arr);
            die();
        } else {
            return $data;
        }
    }

    /** 身份证识别钩子
     * @param array $array $data['ImageBase64'] = $ImageBase64;$data['CardSide'] = 'FRONT';
     * 传入例子   checkOcr($data, $data['CardSide'])
     * @param array $array ['CardSide'] |'FRONT','BACK' 识别身份证正反
     * @return array|mixed
     */
    public function checkOcr($arrjson)
    {
        $arr['time'] = time();
        $data = Card::checkcardocr(json_encode($arrjson), $arrjson['CardSide']);
        if (isset($data['msg'])) {
            $arr["msg"] = $data['msg'];
            $arr['code'] = $data['code'];
            $arr["data"] = $data['data'];
            echo json_encode($arr);
            die();
        } else {
            return $data;
        }
    }

    /**
     * 添加命名空间
     */
    public function appInit()
    {
        //添加支付包的命名空间
        Loader::addNamespace('TencentCloud', ADDON_PATH . 'cardocr' . DS . 'library' . DS . 'TencentCloud' . DS);
    }

    /**
     * 会员中心边栏后
     * @return mixed
     * @throws \Exception
     */
    public function userSidenavAfter()
    {
        $request = Request::instance();
        $controllername = strtolower($request->controller());
        $actionname = strtolower($request->action());
        $data = [
            'actionname'     => $actionname,
            'controllername' => $controllername
        ];
        return $this->fetch('view/hook/user_sidenav_after', $data);
    }
}
